import { translateFormsToFront,
    translateFormsToEnd } from "form.js"

import { getEnvelopeDocuments } from "@interfaces/envelopes/documents.js"
import { getEnvelopeFroms,
    createEnvelopeFroms,
    updateEnvelopeFroms,
    deleteEnvelopeFroms } from "@interfaces/envelopes/forms.js"

import { getParticipantEnvelopeFroms,
    createParticipantEnvelopeFroms,
    updateParticipantEnvelopeFroms,
    deleteParticipantEnvelopeFroms } from "@interfaces/envelopes/participants/forms.js"


class FormsManager{
    constructor(){
        this.newForms = []
        this.oldForms = []
        this.envelopeWsid = ""
        this.participantWsid = ""
    }

    //筛选Forms的类型
    getForms(){
        return this.newForms.filter(form => {
            if (form.type === "TEXT"
                || form.type === "DATE"
                || form.type === "SEAL"
                || form.type === "SIGN") return true
            return false
        })
    }

    //获取骑缝章
    getPagingSeals(){
        return this.newForms.filter(form => {
            if (form.type === "MULTI_CHECK_MARK") return true
            return false
        })
    }

    appendForm(form){
        this.newForms.push(form)
    }


    deleteForm(formRandom){
        let index = this.newForms.findIndex(form => form.random === formRandom)
        if (index > -1) this.newForms.splice(index, 1)
    }

    load(envelopeWsid, participantWsid){
        this.envelopeWsid = envelopeWsid
        this.participantWsid = participantWsid

        return this.loadFormsData(envelopeWsid, participantWsid)
    }

    loadFormsData(envelopeWsid, participantWsid){
        let docMap = {}

        return getEnvelopeDocuments({
            envelopeWsid
        }).then(res => {
            res.data.data.documents.forEach(doc => {
                docMap[doc.fileWsid] = true
            })
            return this.getFormsInterface().getEnvelopeFroms({
                envelopeWsid,
                participantWsid
            })
        }).then(res => {
            let forms = translateFormsToFront(res.data.data.forms)
            //根据参与方的id来筛选他可以编辑修改的form？
            if (participantWsid) forms = forms.filter(form => form.participantWsid === participantWsid)

            this.newForms = forms.filter(form => docMap[form.docId])
            this.oldForms = translateFormsToFront(translateFormsToEnd(forms))
        })
    }

    uploadFormsData(){
        let oldforms = this.oldForms
        let forms = this.newForms

        let envelopeWsid = this.envelopeWsid
        let participantWsid = this.participantWsid

        let news = forms.filter(form => form.id === "")
        let exists = forms.filter(oldform => {
            return oldforms.find(form => form.id === oldform.id)
        })
        let deletes = oldforms.filter(oldform => {
            return !forms.find(form => form.id === oldform.id)
        })

        let updates = exists.filter(form => {
            let oldForm = oldforms.find(old => old.id === form.id)
            if (form.sameAs(oldForm)){
                return false
            }
            return true
        })

        let createHandle = null
      //若news数组不为空，调用新增接口，之后则把新增的forms更新到oldForms中
        if (news.length > 0){
            createHandle = this.getFormsInterface().createEnvelopeFroms({
                envelopeWsid,
                participantWsid,
                forms: translateFormsToEnd(news)
            }).then(res => {
                let forms = res.data.data.forms
                forms.forEach(newForm => {
                    let form = news.find(form => form.random == newForm.tagId)
                    form.id = newForm.formWsid
                    this.oldForms.push(...translateFormsToFront(translateFormsToEnd([form])))
                })
            }).catch(err => {
                // TODO: 错误处理
                return
            })
        }

        let updateHandle = null
      //若updates数组不为空，调用更新接口，之后则把更新的forms更新到oldForms中
        if (updates.length > 0){
            updateHandle = this.getFormsInterface().updateEnvelopeFroms({
                envelopeWsid,
                participantWsid,
                forms: translateFormsToEnd(updates)
            }).then(res => {
                updates.forEach(form => {
                    let oldIndex = this.oldForms.findIndex(oldForm => form.id === oldForm.id)
                    this.oldForms.splice(oldIndex, 1, ...translateFormsToFront(translateFormsToEnd([form])))
                })
            }).catch(err => {
                // TODO: 错误处理
                return
            })
        }

        let deleteHandle = null
        if (deletes.length > 0){
            deleteHandle = this.getFormsInterface().deleteEnvelopeFroms({
                envelopeWsid,
                participantWsid,
                forms: translateFormsToEnd(deletes).map(form => {
                    return {
                        formWsid: form.formWsid
                    }
                })
            }).then(res => {
                deletes.forEach(form => {
                    let oldIndex = this.oldForms.findIndex(oldForm => form.id === oldForm.id)
                    this.oldForms.splice(oldIndex, 1)
                })
            }).catch(err => {
                // TODO: 错误处理
                return
            })
        }

        return Promise.all([createHandle, updateHandle, deleteHandle])
    }

    //获取form有关的接口，但为什么要用participantWsid来判断呢？
    getFormsInterface(){
        if (this.participantWsid) {
            return {
                getEnvelopeFroms: getParticipantEnvelopeFroms,
                createEnvelopeFroms: createParticipantEnvelopeFroms,
                updateEnvelopeFroms: updateParticipantEnvelopeFroms,
                deleteEnvelopeFroms: deleteParticipantEnvelopeFroms
            }
        } else {
            return {
                getEnvelopeFroms: getEnvelopeFroms,
                createEnvelopeFroms: createEnvelopeFroms,
                updateEnvelopeFroms: updateEnvelopeFroms,
                deleteEnvelopeFroms: deleteEnvelopeFroms
            }
        }
    }

}

export {
    FormsManager
}
